<template>
  <sn-three-col-space :side-content="9" :content="6">
    <el-form
        ref="ruleForm"
        class="register"
        :rules="rules"
        slot="spaceContent"
        label-position="left"
        label-width="100px"
        :model="ruleForm">
      <el-form-item label="手机号" prop="phoneNumber">
        <el-input v-model="ruleForm.phoneNumber"></el-input>
        <el-button
            @click="toSendMessage"
            type="info"
            class="margin-top-20">
          <sn-text :text="sendMessage"/>
        </el-button>
      </el-form-item>
      <el-form-item label="短信动态码" prop="code">
        <el-input v-model="ruleForm.code"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
            type="password"
            v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="ensurePassword">
        <el-input
            type="password"
            v-model="ruleForm.ensurePassword"></el-input>
      </el-form-item>
      <el-button
          @click="submitForm('ruleForm')"
          type="danger" style="width: 100%">
        同意以下协议并注册
      </el-button>
      <sn-text
          class="margin-top-20"
          :font-size="16"
          text="《future影院服务协议》"
          font-color="red"/>
    </el-form>
  </sn-three-col-space>
</template>

<script>
export default {
  name: "Register",
  data() {
    return {
      countDown: 60,
      countDownInfo: ' 秒后再次发送',
      sendMessage: '免费获取短信动态码',
      ruleForm: {
        phoneNumber: '',
        code: '',
        password: '',
        ensurePassword: '',
      },
      rules: {
        phoneNumber: [
          {required: true, message: '请输入手机号码', trigger: 'blur'},
        ],
        code: [
          {required: true, message: '请输入验证码', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ],
        ensurePassword: [
          {required: true, message: '请确认密码', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    toSendMessage() {
      this.$http({
        url: '/web/user/login/sms/sendCode',
        method: 'get',
        params: this.$http.adornParams({
          'phone': this.ruleForm.phoneNumber,
          'password': this.ruleForm.password,
          'code': this.ruleForm.message
        })
      })
      const messageTimer = setInterval(() => {
        this.countDown = this.countDown - 1
        this.sendMessage = this.countDown + this.countDownInfo
        if (this.countDown === 0) {
          clearInterval(messageTimer)
          this.countDown = 60
          this.sendMessage = '免费获取短信动态码'
        }
      }, 1000)
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$http({
            url: '/web/user/regist',
            method: 'post',
            data: this.$http.adornData({
              'phone': this.ruleForm.phoneNumber,
              'password': this.ruleForm.password,
              'code': this.ruleForm.code
            })
          }).then(({data}) => {
            if (data.code === 0) {
              this.$router.replace({
                name: 'login'
              })
              this.$message({
                message: '恭喜你，注册成功',
                type: 'success'
              });
            } else {
              this.myUtils.showMessage(this, data.msg)
            }
          })
        } else {
          return false;
        }
      });
    },
  },
}
</script>

<style scoped>
.register {
  margin-top: 60px;
}
</style>